import sys

class Solution(object):
    def nthSuperUglyNumber(self, n, primes):
        """
        :type n: int
        :type primes: List[int]
        :rtype: int
        """
        indices = [0] * len(primes)
        ugly = [sys.maxsize]*n
        ugly[0] = 1
        for i in range(1, n):
            for j in range(len(primes)):
                ugly[i] = min(ugly[indices[j]] * primes[j], ugly[i])
            for j in range(len(primes)):
                if ugly[indices[j]] * primes[j] == ugly[i]:
                    indices[j]+=1
        return ugly[-1]

        
print(Solution().nthSuperUglyNumber(12, [2,7,13,19]))
